package com.liuhung.ash.fang.foreController;

import com.liuhung.ash.fang.entity.*;
import com.liuhung.ash.fang.page.*;
import com.liuhung.ash.fang.service.*;
import com.liuhung.base.util.HtmlUtil;
import com.liuhung.base.web.BaseAction;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <b>项目Controller</b>
 * <b>ProjectController<br>
 */ 
@Controller
@RequestMapping("/f/project")
public class ForeProjectController extends BaseAction{
	
	private final static Logger log= Logger.getLogger(ForeProjectController.class);
	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private ProjectService projectService;

	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private HuxingService huxingService;

	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private LibaoService libaoService;

	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private YouhuiService youhuiService;

	@Autowired(required=false) //注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TuceService tuceService;
	/**
	 * list页面
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping
	public ModelAndView  list(ProjectPage page,HttpServletRequest request) throws Exception{
		Map<String,Object>context = new HashMap<String,Object>();
		List<Project> projects = projectService.queryByListWithAll(page);
		context.put("projects",projects);
		return forword("index",context);
	}

	@RequestMapping("/kinghu")
	public ModelAndView  kinghu(ProjectPage page,HttpServletRequest request) throws Exception{
		Map<String,Object>context = new HashMap<String,Object>();
		List<Project> projects = projectService.queryByListWithAll(page);
		context.put("projects",projects);
		return forword("kinghu",context);
	}

	@RequestMapping("/youhui")
	public ModelAndView  youhui(ProjectPage page,HttpServletRequest request) throws Exception{
		Map<String,Object>context = new HashMap<String,Object>();
		List<Project> projects = projectService.queryByListWithAll(page);
		context.put("projects",projects);
		return forword("youhui",context);
	}

	@RequestMapping("/libao")
	public ModelAndView  libao(ProjectPage page,HttpServletRequest request) throws Exception{
		Map<String,Object>context = new HashMap<String,Object>();
		List<Project> projects = projectService.queryByListWithAll(page);
		context.put("projects",projects);
		return forword("libao",context);
	}

	@RequestMapping("tuijian")
	public ModelAndView  tuijian(ProjectPage page,HttpServletRequest request) throws Exception{
		Map<String,Object>context = new HashMap<String,Object>();
		List<Project> projects = projectService.queryByListWithAll(page);
		context.put("projects",projects);
		return forword("tuijian",context);
	}
	@RequestMapping("xinjing")
	public ModelAndView  xinjing(ProjectPage page,HttpServletRequest request) throws Exception{
		Map<String,Object>context = new HashMap<String,Object>();
		page.setOrder(1);
		page.setSort("datecreated");
		List<Project> projects = projectService.queryByListWithAll(page);
		context.put("projects",projects);
		return forword("xinjing",context);
	}
	@RequestMapping("rexiao")
	public ModelAndView  rexiao(ProjectPage page,HttpServletRequest request) throws Exception{
		Map<String,Object>context = new HashMap<String,Object>();
		List<Project> projects = projectService.queryByListWithAll(page);
		page.setOrder(1);
		page.setSort("datecreated");
		context.put("projects",projects);
		return forword("rexiao",context);
	}

	@RequestMapping("/search")
	public ModelAndView  search(ProjectPage page,Integer type,HttpServletRequest request) throws Exception{
		Map<String,Object>context = new HashMap<String,Object>();
		if (type==1)
		{
			return forword("search/area",null);
		}
		else if (type==2)
		{
			return forword("search/address",null);
		}
		else if (type==3)
		{
			return forword("search/price",null);
		}
		else if (type==4)
		{
			return forword("search/hx",null);
		}
		else{
			List<Project> projects = projectService.queryByListWithAll(page);
			context.put("projects",projects);
			return forword("index",null);
		}
	}


	@RequestMapping("/{id}")
	public ModelAndView  rexiao(@PathVariable Integer id,HttpServletRequest request) throws Exception{
		Map<String,Object> context = new HashMap<String,Object>();
		Project project = projectService.queryById(id);
		if (project ==null){
			throw new NoSuchRequestHandlingMethodException(request);
		}
		context.put("project", project);

		HuxingPage huxingPage= new HuxingPage();
		huxingPage.setRows(1000);
		huxingPage.setProjectid(id);
		List<Huxing> huxings = huxingService.queryByList(huxingPage);
		context.put("huxings", huxings);

		LibaoPage libaoPage= new LibaoPage();
		libaoPage.setRows(1000);
		libaoPage.setProjectid(id);
		List<Libao> libaos = libaoService.queryByList(libaoPage);
		context.put("libaos", libaos);

		YouhuiPage youhuiPage= new YouhuiPage();
		youhuiPage.setRows(1000);
		youhuiPage.setProjectid(id);
		List<Youhui> youhuis = youhuiService.queryByList(youhuiPage);
		context.put("youhuis", youhuis);

		TucePage tucePage = new TucePage();
		tucePage.setProjectid(id);
		tucePage.setRows(1000);
		List<Tuce> tuces = tuceService.queryByList(tucePage);
		context.put("tuces",tuces);
		return forword("project",context);
	}
}
